﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conference.Logic.Proposal;
using Conference.Logic.Base;

namespace Conference.Logic.Schedule
{
    /// <summary>
    /// Class that represents a moorning session
    /// </summary>
    public class Moorning : Session
    {
        #region Attributes
        private TimeSpan freeTime;
        private bool isFull;
        #endregion

        #region Properties
        /// <summary>
        /// Read only property that returns a list of item session for this moorning
        /// </summary>
        public List<ItemSession> Sessions 
        {
            get 
            {
                return base.sessions;
            }
        }

        public bool IsFull
        {
            get 
            {
                return isFull;
            }
        }

        public TimeSpan FreeTime
        {
            get
            {
                return freeTime;
            }
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Create a new instance of a mooring object based on session class
        /// </summary>
        public Moorning() : base()
        {
            this.isFull = false;
            this.freeTime = (Parameter.Default.FinishMoorning - Parameter.Default.StartMorning);
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add new item on base class and calculate free time.
        /// </summary>
        /// <param name="item">ItemSession object</param>
        /// <returns>True or false</returns>
        public override bool Add(ItemSession item)
        {
            this.freeTime = this.freeTime.Subtract(item.Duration);
            return base.Add(item);
        }

        /// <summary>
        /// Add new item on base class and calculate free time
        /// </summary>
        /// <param name="description">Title</param>
        /// <param name="duration">Length duration</param>
        /// <param name="startTime">Start time</param>
        /// <returns>True or false</returns>
        public override bool Add(string description, ItemType type, TimeSpan startTime)
        {
            if (type == ItemType.Lightning)
                this.freeTime = this.freeTime.Subtract(Parameter.Default.LenghtLightning);
            return base.Add(description, type, startTime);
        }

        /// <summary>
        /// Add new item on base class and calculate free time
        /// </summary>
        /// <param name="description">Title</param>
        /// <param name="type">Type of item (default, ligthning)</param>
        /// <param name="startTime">Start time</param>
        /// <returns>True or false</returns>
        public override bool Add(string description, TimeSpan duration, TimeSpan startTime)
        {
            this.freeTime = this.freeTime.Subtract(duration);
            return base.Add(description, duration, startTime);
        }

        /// <summary>
        /// Fill moorning instance with available proposals
        /// </summary>
        /// <param name="list"></param>
        public void Fill(List<ItemProposal> list)
        {
            try
            {
                foreach (ItemProposal item in list)
                {
                    if (this.IsFull)
                        break;

                    //it continues because if exists other item with small duration, can fit here. 
                    if (this.FreeTime.TotalMinutes < item.Duration.TotalMinutes)
                        continue;
                    else
                    {
                        if (item.Type == ItemType.Lightning)
                            this.Add(item.Description, ItemType.Lightning, base.GetNextSession());
                        else
                            this.Add(item.Description, item.Duration, base.GetNextSession());

                        item.IsScheduled = true;
                    }
                }

                // add a lauch time to moorning end
                this.Add("Launch", ItemType.Launch, Parameter.Default.StartLauch);
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        #endregion
    }
}
